package com.chinatechstar.base.controller;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chinatechstar.base.entity.SysArea;
import com.chinatechstar.base.service.SysAreaService;
import com.chinatechstar.component.commons.result.ActionResult;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.chinatechstar.component.commons.validator.InsertValidator;
import com.chinatechstar.component.commons.validator.UpdateValidator;

/**
 * 基础数据-区块 的控制层
 *
 * @author Rei
 */
@Api(value = "基础数据-区块 的控制层", tags = {"基础数据-区块"})
@RestController
@RequestMapping("/sysarea")
public class SysAreaController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private SysAreaService sysAreaService;

    /**
     * 查询 基础数据-区块 所有数据
     *
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询 基础数据-区块 所有数据", tags = {"基础数据-区块"})
    @GetMapping(path = "/findSysAreaAll")
    public ListResult<Object> findSysAreaAll() {
        List<SysArea> list = sysAreaService.findSysAreaAll();
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 查询 基础数据-区块 分页
     *
     * @param currentPage 当前页数
     * @param pageSize    每页记录数
     * @param areaName    区块名称
     * @param areaOrgId   生产单位ID
     * @param sorter      排序
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询 基础数据-区块 分页", tags = {"基础数据-区块"})
    @GetMapping(path = "/findSysAreaByCondition")
    public ListResult<Object> findSysAreaByCondition(@RequestParam(name = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                                     @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                     @RequestParam(name = "areaName", required = false) String areaName,
                                                     @RequestParam(name = "areaOrgId", required = false) Long areaOrgId,
                                                     @RequestParam(name = "sorter", required = false) String sorter) {
        List<LinkedHashMap<String, Object>> data = sysAreaService.findSysAreaByCondition(currentPage, pageSize, areaName, areaOrgId, sorter);
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 根据查询条件导出 基础数据-区块
     *
     * @param response 响应对象
     * @param paramMap 参数Map
     */
    @ApiOperation(value = "根据查询条件导出 基础数据-区块", tags = {"基础数据-区块"})
    @PostMapping(path = "/exportSysArea")
    public void exportSysArea(HttpServletResponse response, @RequestParam Map<String, Object> paramMap) {
        try {
            List<String> headList = Arrays.asList("ID", "名称", "创建时间");
            List<LinkedHashMap<String, Object>> dataList = sysAreaService.findSysAreaForExcel(paramMap);
            ExcelUtils.exportExcel(headList, dataList, "管理", response);
        } catch (Exception e) {
            logger.warn(e.toString());
        }
    }

    /**
     * 根据主键ID查询 基础数据-区块 数据
     *
     * @param id ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数据查询 基础数据-区块 数据", tags = {"基础数据-区块"})
    @GetMapping(path = "/findSysAreaById")
    public ActionResult findSysAreaById(@RequestParam(name = "id") Long id) {
        SysArea data = sysAreaService.findSysAreaById(id);
        return ResultBuilder.buildActionSuccess(data);
    }

    /**
     * 根据主键ID数组查询 基础数据-区块 数据
     *
     * @param ids ID数组
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数组查询 基础数据-区块 数据", tags = {"基础数据-区块"})
    @GetMapping(path = "/findSysAreaByIds")
    public ListResult<Object> findSysAreaByIds(@RequestParam(name = "ids") Long[] ids) {
        List<SysArea> list = sysAreaService.findSysAreaByIds(ids);
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 新增 基础数据-区块
     *
     * @param sysArea 基础数据-区块 实体
     * @return ActionResult
     */
    @ApiOperation(value = "新增 基础数据-区块", tags = {"基础数据-区块"})
    @PostMapping(path = "/addSysArea")
    public ActionResult addSysArea(@Validated(InsertValidator.class) @RequestBody SysArea sysArea) {
        sysAreaService.insertSysArea(sysArea);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 批量新增 基础数据-区块
     *
     * @param sysAreas 基础数据-区块 实体数组
     * @return ActionResult
     */
    @ApiOperation(value = "批量新增 基础数据-区块", tags = {"基础数据-区块"})
    @PostMapping(path = "/addSysAreas")
    public ActionResult addSysAreas(@Validated(InsertValidator.class) @RequestBody List<SysArea> sysAreas) {
        sysAreaService.insertSysAreas(sysAreas);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 编辑 基础数据-区块
     *
     * @param sysArea 基础数据-区块 实体
     * @return ActionResult
     */
    @ApiOperation(value = "编辑 基础数据-区块", tags = {"基础数据-区块"})
    @PutMapping(path = "/updateSysArea")
    public ActionResult updateSysArea(@Validated(UpdateValidator.class) @RequestBody SysArea sysArea) {
        sysAreaService.updateSysArea(sysArea);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 批量编辑 基础数据-区块
     *
     * @param sysAreas 基础数据-区块 实体数组
     * @return ActionResult
     */
    @ApiOperation(value = "批量编辑 基础数据-区块", tags = {"基础数据-区块"})
    @PostMapping(path = "/updateSysAreas")
    public ActionResult updateSysAreas(@Validated(UpdateValidator.class) @RequestBody List<SysArea> sysAreas) {
        sysAreaService.updateSysAreas(sysAreas);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 删除 基础数据-区块
     *
     * @param id ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID删除 基础数据-区块", tags = {"基础数据-区块"})
    @PostMapping(path = "/deleteSysAreaById")
    public ActionResult deleteSysAreaById(@RequestParam(name = "id") Long id) {
        sysAreaService.deleteSysAreaById(id);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 删除 基础数据-区块
     *
     * @param ids ID数组
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数组批量删除 基础数据-区块", tags = {"基础数据-区块"})
    @PostMapping(path = "/deleteSysAreaByIds")
    public ActionResult deleteSysAreaByIds(@RequestParam(name = "id") Long[] ids) {
        sysAreaService.deleteSysAreaByIds(ids);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 软删除 基础数据-区块
     *
     * @param id ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID软删除 基础数据-区块", tags = {"基础数据-区块"})
    @PostMapping(path = "/softDeleteSysAreaById")
    public ActionResult softDeleteSysAreaById(@RequestParam(name = "id") Long id) {
        sysAreaService.softDeleteSysAreaById(id);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 软删除 基础数据-区块
     *
     * @param ids ID数组
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数组批量软删除 基础数据-区块", tags = {"基础数据-区块"})
    @PostMapping(path = "/softDeleteSysAreaByIds")
    public ActionResult softDeleteSysAreaByIds(@RequestParam(name = "id") Long[] ids) {
        sysAreaService.softDeleteSysAreaByIds(ids);
        return ResultBuilder.buildActionSuccess();
    }

}
